﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SavingStudentInBinaryFile
{
    class Student
    {
        private string firstName;
        private string lastName;
        private int facultyNumber;

        public Student(string firstName, string lastName, int facultyNumber) 
        {
            this.firstName = firstName;
            this.lastName = lastName;
            this.facultyNumber = facultyNumber;
        }

        public static void AddStudent(BinaryWriter file, Student pesho) 
        {
            file.Write(pesho.FirstName);
            file.Write(pesho.LastName);
            file.Write(pesho.FacultyNumber);
        }

        
        public static void DeleteStudent(int num)
        {
            BinaryReader reader = new BinaryReader(File.Open("students.bin", FileMode.Open));
            using (reader)
            {
                BinaryWriter writer = new BinaryWriter(File.Open("students2.bin", FileMode.Create));
                using (writer)
                {
                    string name;
                    string second;
                    int number;

                    while (reader.BaseStream.Position < reader.BaseStream.Length)
                    {
                        name = reader.ReadString();
                        second = reader.ReadString();
                        number = reader.ReadInt32();
                        if (number != num)
                        {
                            writer.Write(name);
                            writer.Write(second);
                            writer.Write(number);
                        }
                    }
                }
            }
            File.Delete("students.bin");
            File.Move("students2.bin", "students.bin");
        }

        public int FacultyNumber
        {
            get
            {
                return facultyNumber;
            }
            set
            {
                facultyNumber = value;
            }
        }

        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                lastName = value;
            }
        }

        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
            }
        }
    }
}
